package com.z.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.z.common.context.UserContext;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import com.z.sys.entity.Notice;
import com.z.sys.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 消息通知Controller
 *
 * @author z
 * @date 2022-12-09
 */
@RestController
@RequestMapping("/system/notice")
public class NoticeController extends BaseController {

    private String prefix = "system/notice";

    @Autowired
    private INoticeService noticeService;

    @GetMapping("/main")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询消息通知列表
     */
    @ResponseBody
    @GetMapping("/data")
    public Result list(@ModelAttribute Notice notice, PageVo pageVo) {
        PageInfo<Notice> pageInfo = noticeService.selectNoticePage(notice, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 新增消息通知
     */
    @GetMapping("/add")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增消息通知
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@RequestBody Notice notice) {
        return Result.decide(noticeService.save(notice));
    }

    /**
     * 修改消息通知
     */
    @GetMapping("/edit")
    public ModelAndView edit(String id, ModelMap map) {
        Notice notice =noticeService.getById(id);
        map.put("notice", notice);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改消息通知
     */
    @ResponseBody
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice) {
        return Result.decide(noticeService.updateById(notice));
    }

    /**
     * 删除消息通知
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    public Result batchRemove(String ids) {
        return Result.decide(noticeService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除消息通知
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") String id) {
        return Result.decide(noticeService.removeById(id));
    }

    /**
     * 查询消息
     */
    @ResponseBody
    @GetMapping("message")
    public List<Map> notice() {

        List<Map> result = new ArrayList<>();

        Notice publicParam = new Notice();
        publicParam.setType("public");

        Notice privateParam = new Notice();
        privateParam.setType("private");
        privateParam.setAccept(UserContext.currentUser().getId() + "");

        Notice noticeParam = new Notice();
        noticeParam.setType("notice");

        Map<String, Object> publicArray = new HashMap<>(3);
        publicArray.put("id", "1");
        publicArray.put("title", "公告");
        publicArray.put("children", noticeService.list(new QueryWrapper<Notice>().eq("type", "public")));

        Map<String, Object> privateArray = new HashMap<>(3);
        privateArray.put("id", "2");
        privateArray.put("title", "私信");
        privateArray.put("children", noticeService.list(new QueryWrapper<Notice>().eq("type", "private")));

        Map<String, Object> noticeArray = new HashMap<>(3);
        noticeArray.put("id", "3");
        noticeArray.put("title", "通知");
        noticeArray.put("children", noticeService.list(new QueryWrapper<Notice>().eq("type", "system")));

        result.add(publicArray);
        result.add(privateArray);
        result.add(noticeArray);

        return result;
    }
}
